import java.io.*;
import java.net.*;


public class Server{
	//for listening
	private static ServerSocket serverSocket;
	//for connecting
	static Socket connection = null;
	private static int portNo = 0;
	private static InetAddress client;
	private static String pathname = null;
	
	
    public static void main(String[] args) throws IOException {
		if(args.length == 1){
			portNo = Integer.parseInt(args[0]); 
			pathname = "no pathname"; 
		}
		else if(args.length == 2){
			portNo = Integer.parseInt(args[0]); 
			pathname = args[1];
		}
		
		System.out.println("connected on port number: " + portNo);
		System.out.println("the path name is: " + pathname);
		
		
    	serverSocket = new ServerSocket(portNo);  //bind and listen on port 1024
      
    	
    	while(true) {
    		try{

    			connection = serverSocket.accept(); //connect a client
    			connection.setSoTimeout(10000); 
    			client = connection.getInetAddress(); //maintain ip address
    			System.out.println("Server is: " + client.getHostName());
    			if(pathname.equals("no pathname")){
    				//System.out.println("calling in here 111111");
    				new Client(connection, client);
    			}
    			else{
    				//System.out.println("calling in here 2222222");
        			new Client(connection, client, pathname);   //create new client thread
    			}
    		}
    		catch(Exception ex){
    			System.out.println(ex);
    		}
    	
    	}
    	
    }
    
}